<template>
  <div style="height: 100%">
    <el-scrollbar style="height: 100%">



      <el-row class="add_el_row_wrapper" style="background: white;margin: 0;padding-top: 18px;padding-bottom: 18px">
        <el-col :span="15" class="add_el_button_el_col">

        </el-col>
        <el-col :span="9" class="right_tool_el_col">

          <div style="width: 100%;text-align: right">
            <el-radio-group @change="thisPageState.radioDataQuery.change" v-model="thisPageState.radioDataQuery.state">
              <el-radio-button v-for="(item,index) in thisPageState.radioDataQuery.list" :key="index" :label="item.label" :value="item.value" />
            </el-radio-group>
          </div>
          <div style="margin-right: 20px">

          </div>
        </el-col>
      </el-row>


      <el-row v-show="showSearch" class="query_el_row-wrapper" v-if="thisPageState.radioDataQuery.state===thisPageState.radioDataQuery.list[1].value"
              style="margin: 0;padding-bottom: 18px;padding-top: 0">
        <el-col :span="17">
          <el-form :model="state.queryForm" ref="queryRef" :inline="true" @keyup.enter="queryDataList">
            <el-form-item label="创建时间" prop="createTime">
              <el-date-picker type="datetime" placeholder="请输入创建时间" v-model="state.queryForm.createTime"></el-date-picker>
            </el-form-item>
          </el-form>
        </el-col>

        <el-col :span="7">
          <div class="query_button_wrap">
            <el-button icon="search" type="primary" @click="queryDataList">
              查询
            </el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </div>

        </el-col>
      </el-row>
<!--      原始样式-->
      <el-row v-show="showSearch" v-if="false">
        <el-col :span="12" v-if="thisPageState.radioDataQuery.state===thisPageState.radioDataQuery.list[1].value">
          <el-form :model="state.queryForm" ref="queryRef" :inline="true" @keyup.enter="queryDataList">
            <el-form-item label="创建时间" prop="createTime">
              <el-date-picker type="datetime" placeholder="请输入创建时间" v-model="state.queryForm.createTime"></el-date-picker>
            </el-form-item>
            <el-form-item>
              <el-button icon="search" type="primary" @click="queryDataList">
                查询
              </el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="thisPageState.radioDataQuery.state===thisPageState.radioDataQuery.list[1].value ? 12 : 24">
          <div style="width: 100%;text-align: right">
            <el-radio-group @change="thisPageState.radioDataQuery.change" v-model="thisPageState.radioDataQuery.state">
              <el-radio-button v-for="(item,index) in thisPageState.radioDataQuery.list" :key="index" :label="item.label" :value="item.value" />
            </el-radio-group>
          </div>
        </el-col>
      </el-row>


<!--      最新数据展示内容-->
      <div v-if="thisPageState.radioDataQuery.state===thisPageState.radioDataQuery.list[0].value" >
        <duorou-kgnpk-dev-data-info :duorouKgnpkDevData="null" :iotDevice="props.iotDevice"></duorou-kgnpk-dev-data-info>
      </div>
<!--      历史追溯展示内容-->
     <div v-if="thisPageState.radioDataQuery.state===thisPageState.radioDataQuery.list[1].value">

       <el-row class="add_el_row_wrapper" style="background: white;margin: 0;padding-bottom: 18px;padding-left: 20px">
         <el-col :span="15" class="add_el_button_el_col">
           <el-button plain :disabled="multiple" icon="Delete" type="primary"
                      @click="handleDelete(selectObjs)">
             删除
           </el-button>
         </el-col>
         <el-col :span="9" class="right_tool_el_col">
           <div style="margin-right: 10px"></div>
           <right-toolbar v-model:showSearch="showSearch" :export="'duorou_duorouKgnpkDevData_export'"
                          @exportExcel="exportExcel" class="ml10 mr20" style="float: right;"
                          @queryTable="queryDataList"></right-toolbar>
         </el-col>
       </el-row>

       <el-row v-if="false">
         <div class="mb8" style="width: 100%">
           <el-button plain :disabled="multiple" icon="Delete" type="primary"
                      @click="handleDelete(selectObjs)">
             删除
           </el-button>
           <right-toolbar v-model:showSearch="showSearch" :export="'duorou_duorouKgnpkDevData_export'"
                          @exportExcel="exportExcel" class="ml10 mr20" style="float: right;"
                          @queryTable="queryDataList"></right-toolbar>
         </div>
       </el-row>


       <el-table  :data="state.dataList" v-loading="state.loading"
                 :cell-style="tableStyle.cellStyle" :header-cell-style="tableStyle.headerCellStyle"
                 @selection-change="selectionChangHandle"
                 @sort-change="sortChangeHandle">
         <el-table-column type="selection" width="40" align="center"/>
         <el-table-column type="index" label="#" width="40"/>
         <!--        <el-table-column prop="iotDeviceId" label="对应的物联设备ID" show-overflow-tooltip/>-->
         <el-table-column prop="createTime" label="创建时间" sortable="custom" show-overflow-tooltip/>
         <el-table-column label="操作" width="300">
           <template #default="scope">
             <el-button icon="Histogram" text type="primary" @click="detailDataDialogRef.openDialog(scope.row)">详细数据</el-button>
             <el-button icon="delete" text type="primary" @click="handleDelete([scope.row.id])">删除</el-button>
           </template>
         </el-table-column>
       </el-table>
       <pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination"/>
     </div>
    </el-scrollbar>

    <!-- 查询详细数据  -->
    <detail-data-dialog ref="detailDataDialogRef" />

  </div>




</template>

<script setup lang="ts" name="systemDuorouKgnpkDevDataCap">
import {BasicTableProps, useTable} from "/@/hooks/table";
import {fetchList, delObjs} from "/@/api/duorou/duorouKgnpkDevData";
import {useMessage, useMessageBox} from "/@/hooks/message";
import {useDict} from '/@/hooks/dict';

const { proxy } = getCurrentInstance();


/**
 * 定义组件属性 接收传递来的 大棚ID
 */
const props = defineProps({
  iotDevice: {
    type:Object,
    default: () => {},
  },
})


// 引入详细数据对话框组件
const DetailDataDialog = defineAsyncComponent(() => import('./detailDataDialog.vue'));

// 引入 氮磷钾数据检测数据详细信息 组件
const DuorouKgnpkDevDataInfo = defineAsyncComponent(() => import('/@/views/iot/iotDevice/deviceRunData/duorouKgnpkDevData/duorouKgnpkDevDataInfo/index.vue'));


// 定义查询字典

// 定义变量内容
const detailDataDialogRef = ref()
// 搜索变量
const queryRef = ref()
const showSearch = ref(true)
// 多选变量
const selectObjs = ref([]) as any
const multiple = ref(true)




const state: BasicTableProps = reactive<BasicTableProps>({
  queryForm: {
    iotDeviceId:props.iotDevice.id,
  },
  pageList: fetchList
})

//  table hook
const {
  getDataList,
  currentChangeHandle,
  sizeChangeHandle,
  sortChangeHandle,
  downBlobFile,
  tableStyle
} = useTable(state)

// 清空搜索条件
const resetQuery = () => {
  // 清空搜索条件
  queryRef.value?.resetFields()
  // 清空多选
  selectObjs.value = []
  queryDataList()
}

// 导出excel
const exportExcel = () => {
  downBlobFile('/duorou/duorouKgnpkDevData/export', Object.assign(state.queryForm, {ids: selectObjs}), 'duorouKgnpkDevData.xlsx')
}

// 多选事件
const selectionChangHandle = (objs: { id: string }[]) => {
  selectObjs.value = objs.map(({id}) => id);
  multiple.value = !objs.length;
};

// 删除操作
const handleDelete = async (ids: string[]) => {
  try {
    await useMessageBox().confirm('此操作将永久删除');
  } catch {
    return;
  }

  try {
    await delObjs(ids);
    queryDataList();
    useMessage().success('删除成功');
  } catch (err: any) {
    useMessage().error(err.msg);
  }
};


/**
 * 重写查询方法
 */
function queryDataList(){
  state.queryForm.createTime=proxy.parseTime(state.queryForm.createTime)
  getDataList()
}


/**
 * 自己定义的页面state
 */
const thisPageState=reactive({
  /**
   * 展示 土壤检测设备数据方式，选择最新的还是 历史的
   */
  radioDataQuery:{
    state:"latestData",
    list:[
      {
        label:"最新数据",
        value:"latestData"
      },
      {
        label:"历史追溯",
        value:"historyData"
      }
    ],
    change:(value)=>{
      // thisPageState.radioDataQuery.state=value
      resetQuery()
    }
  },
  latestDuorouDevData:null
})



</script>

<style scoped lang="scss">

</style>